;==================================================
;赤色要塞
COVER_SCROLL_ALBUM      =   35  ;封面滚动音乐集
COVER_SCROLL_TRACK      =   01  ;封面滚动中曲目号

;双截龙2
;COVER_SCROLL_ALBUM      =   22  ;封面滚动音乐集
;COVER_SCROLL_TRACK      =   63;81 ;封面滚动中曲目号

;双截龙2
;COVER_SCROLL_ALBUM      =   23  ;封面滚动音乐集
;COVER_SCROLL_TRACK      =   56;81 ;封面滚动中曲目号

;忍龙2
;COVER_SCROLL_ALBUM      =   07  ;封面滚动音乐集
;COVER_SCROLL_TRACK      =   50;81 ;封面滚动中曲目号

;忍龙3
;COVER_SCROLL_ALBUM      =   08  ;封面滚动音乐集
;COVER_SCROLL_TRACK      =   76

;==================================================
;;RAF世界
;COVER_RESET_ALBUM       =   03
;COVER_RESET_TRACK       =   01  ;封面滚动结束曲目号

;忍龙3
COVER_RESET_ALBUM       =   08
COVER_RESET_TRACK       =   10  ;封面滚动结束曲目号

;==================================================
;赤色要塞
COVER_QUIT_ALBUM       =   35
COVER_QUIT_TRACK       =   02  ;封面退出曲目号
    
;忍龙3    
;COVER_QUIT_ALBUM        =   08  ;封面滚动音乐集
;COVER_QUIT_TRACK        =   66
    
;RAF世界  
;COVER_QUIT_ALBUM       =   03
;COVER_QUIT_TRACK       =   02  ;封面退出曲目号

BLINK_TEXT_INDEX        =   23  ;闪烁文本索引

BLINK_CUSTOM_INTERVAL   =   5   ;自定义文本闪烁速度(0-7)
BLINK_QC_CODE_INTERVAL  =   4   ;二维码闪烁速度(0-7)
BLINK_TEXT_INTERVAL     =   4   ;开始文本闪烁速度(0-7)

COVER_QUIT_TIME         =   90  ;封面退出延时
;赤色要塞: 35,01,03,02
;忍龙3: 08,03,10,04

;==================================================
;Music_Collection_Order;专辑顺序
 ;.DB 01 ;Contra                                             魂斗罗
 ;.DB 02 ;Super Contra                                       超级魂斗罗
 ;.DB 03 ;Raf World                                          RAF世界
 ;.DB 04 ;Kage                                               赤影战士
 ;.DB 05 ;Final Mission                                      最终任务
 ;.DB 06 ;Ninja Gaiden                                       忍者龙剑传
 ;.DB 07 ;Ninja Gaiden 2 - The Dark Sword of Chaos           忍者龙剑传2: 暗黑的邪神剑
 ;.DB 08 ;Ninja Gaiden 3 - The Ancient Ship of Doom          忍者龙剑传3: 黄泉的方舟
 ;.DB 35 ;Bomberman                                          轰炸人 
 ;.DB 36 ;Bomberman 2                                        轰炸人2
 ;.DB 13 ;Rockman                                            洛克人
 ;.DB 14 ;Rockman 2 - Dr. Wily no Nazo                       洛克人2: 威利博士之谜
 ;.DB 15 ;Rockman 3 - Dr. Wily no Saigo!                     洛克人3: 威利博士的末日!?
 ;.DB 16 ;Rockman 4 - Aratanaru Yabou!!                      洛克人4: 新的野心!!
 ;.DB 17 ;Rockman 5 - Blues no Wana!                         洛克人5: 布鲁斯的陷阱!?
 ;.DB 18 ;Rockman 6 - Shijou Saidai no Tatakai!!             洛克人6: 史上最大的战斗!!
 ;.DB 31 ;Castlevania                                        恶魔城
 ;.DB 32 ;Castlevania 2 - Simon's Quest                      恶魔城2: 西蒙的冒险
 ;.DB 33 ;Castlevania 3 - Dracula's Curse                    恶魔城3: 德拉库拉的诅咒
 ;.DB 34 ;Akumajou Special - Boku Dracula Kun                恶魔城外传: 德拉克拉君
 ;.DB 24 ;Double Dragon                                      双截龙
 ;.DB 25 ;Double Dragon 2 - The Revenge                      双截龙2: 复仇
 ;.DB 26 ;Double Dragon 3 - The Rosetta Stone                双截龙3: 罗塞塔之石
 ;.DB 41 ;Battletoads                                        忍者蛙   
 ;.DB 29 ;Battletoads & Double Dragon - The Ultimate Team    忍者蛙与双截龙: 终极团队
 ;.DB 37 ;Destiny of an Emperor                              吞食天地
 ;.DB 38 ;Destiny of an Emperor 2                            吞食天地2
 ;.DB 43 ;Chip 'n Dale Rescue Rangers                        松鼠大作战
 ;.DB 44 ;Chip 'n Dale Rescue Rangers 2                      松鼠大作战2
 ;.DB 19 ;Teenage Mutant Ninja Turtles - Tournament Fighters 忍者神龟: 激龟快打
 ;.DB 46 ;Island                                             冒险岛
 ;.DB 47 ;Island 2                                           冒险岛2
 ;.DB 48 ;Island 3                                           冒险岛3
 ;.DB 49 ;Island 4                                           冒险岛4
 ;.DB 12 ;Jackal                                             赤色要塞
 ;.DB 22 ;Rush'n Attack                                      绿色兵团
 ;.DB 21 ;Salamander                                         沙罗曼蛇
 ;.DB 09 ;Mighty Final Fight                                 快打旋风
 ;.DB 11 ;Getsufuu Maden                                     月风魔传
 ;.DB 10 ;1943 - The Battle of Midway                        1943: 中途岛海战
 ;.DB 27 ;Gun-Nac                                            加纳战机
 ;.DB 28 ;Summer Carnival '92 - Recca                        夏日嘉年华92: 烈火
 ;.DB 20 ;Crisis Force                                       帝国战机
 ;.DB 23 ;Dragon Fighter                                     龙战士
 ;.DB 30 ;Choujin Sentai - Jetman                            鸟人战队: 喷射人
 ;.DB 42 ;Gun.Smoke                                          荒野大镖客
 ;.DB 45 ;Mitsume ga Tooru                                   三目童子
 ;.DB 39 ;Tokkyuu Shirei                                     特救指令
 ;.DB 40 ;Metal Max                                          重装机兵
 ;.DB 50 ;Battle City                                        坦克大战

    .IF COVER_ENABLE
;==================================================
;封面处理
Cover_Process
    JSR Wait_Fot_Vblank
    JSR Init_Cover_Attributes
    
    ;JSR Init_Cover_Palette
    
    ;写入调色板数据
    LDA #25 - 1
    JSR Write_Cover_Text
    
    JSR Init_Cover_Text
    
    ;设置屏幕镜像($50: 水平; $44: 垂直)
    LDA #$50
    STA MAPPER_REG_NT_MAPPING
    
    LDA #$FF
    STA Cover_Mode_Time
    
    ;音乐bank初始化
    LDA #COVER_SCROLL_ALBUM - 1
    JSR Cover_Album_Init
    LDA #COVER_SCROLL_TRACK - 1
    JSR Cover_Music_Init
    
    LDA #$1E
    STA PPU_Mask_Buf
    LDA #$8A
    STA PPU_Ctrl_Buf
    STA PPU_CTRL
    
    JSR .Wait_Press_Start
    RTS

.Wait_Press_Start
    LDA Fast_Item
    AND Fast_Music
    CMP #$FF
    BNE .Quit
    
    LDA Cover_Mode_Time
    BNE .Wait_Press_Start
    
.Quit
    LDA #$00
    STA PPU_Ctrl_Buf
    STA PPU_Mask_Buf
    STA PPU_CTRL
    STA PPU_MASK
    STA PPU_Buf_Pos
    STA Cover_Mode_Time
    
    JSR Sound_Port_Clear
    JSR Vram_Clear
    
    LDA Fast_Item
    PHA
    LDA Fast_Music
    PHA
    JSR Init_Ex_Ram
    PLA
    STA Fast_Music
    PLA
    STA Fast_Item
    
    ;设置屏幕镜像($50: 水平; $44: 垂直)
    LDA #$44
    STA MAPPER_REG_NT_MAPPING

    JSR Sound_Ram_Clear_Part_1
    
.End
    RTS

;==================================================
;封面NMI处理
Nmi_For_Cover
    LDA #$00
    STA PPU_CTRL
    
    JSR FC_PPU_Buffer_Process
    
    LDA PPU_Ctrl_Buf
    STA PPU_CTRL
    
    JSR Blink_Text
    ;JSR Blink_QR_Code
    ;JSR Blink_Custom_Text
    JSR Update_VScroll
    JSR .Quit_Delay
    
    ;处理手柄输入
    JSR Joystick_Proc
    
    LDA Cover_Mode_Time
    CMP #$FF
    BNE .End
    
    LDA Key_Once
    BEQ .End
    LDA PPU_Ctrl_Buf
    AND #$02
    BNE .Reset_Scroll
    LDA Key_Once
    CMP #$10
    BEQ .Quit
    BNE .End

.Reset_Scroll
    JSR Reset_Cover_Scroll
    
.End
    JSR Cover_Music_Play
    INC Nmi_Time_Count
    RTS

.Quit
    LDA PPU_Ctrl_Buf
    AND #$02
    BNE .Reset_Scroll
    LDA #COVER_QUIT_TIME
    STA Cover_Mode_Time
    
    LDA #COVER_QUIT_ALBUM - 1
    JSR Cover_Album_Init
    LDA #COVER_QUIT_TRACK - 1
    JSR Cover_Music_Init
    
    RTS
.Quit_Delay
    LDA Cover_Mode_Time
    CMP #$FF
    BEQ .Quit_Delay_End
    LDA Cover_Mode_Time
    BEQ .Quit_Delay_End
    DEC Cover_Mode_Time
.Quit_Delay_End
    RTS

;==================================================
;封面音乐集切换
Cover_Album_Init
    STA Data_Buf
    ;CMP Music_Item_Cur
    ;BEQ .End
    JSR Sound_Ram_Clear_Part_1
    LDA Data_Buf
    STA Music_Item_Cur
.End
    JSR Init_Sound_Bank
    RTS

;==================================================
;封面音乐切换
Cover_Music_Init
    PHA
    LDA #$0F
    STA $4015
    PLA
    JSR Set_Sound_Init_Port
    JSR Sound_Init
    RTS

;==================================================
;封面音乐播放
Cover_Music_Play
    JSR Set_Sound_Play_Port
    JSR Sound_Play
    RTS

;==================================================
;重置封面滚动
Reset_Cover_Scroll
    LDA #$00
    STA Scroll_V
    LDA #$88
    STA PPU_Ctrl_Buf
    
    LDA #COVER_RESET_ALBUM - 1
    JSR Cover_Album_Init
    LDA #COVER_RESET_TRACK - 1
    JSR Cover_Music_Init
    RTS
    
;==================================================
;更新滚动
Update_VScroll
    LDA PPU_Ctrl_Buf
    AND #$02
    BEQ .End
    INC Scroll_V
    LDA Scroll_V
    CMP #$F0
    BCC .End
    JSR Reset_Cover_Scroll
.End
    RTS

;==================================================
;闪烁文本
Blink_Text
    LDA Cover_Mode_Time
    CMP #$FF
    BNE .Show
    LDA PPU_Ctrl_Buf
    AND #$02
    BNE .End
    LDA Nmi_Time_Count
    AND #(1 << BLINK_TEXT_INTERVAL) - 1
    BNE .End
    LDA Nmi_Time_Count
    LDX #BLINK_TEXT_INTERVAL
    BEQ .Check
.Shift
    LSR A
    DEX
    BNE .Shift
.Check
    AND #$01
    BEQ .Show
.Clear
    LDA #BLINK_TEXT_INDEX - 1
    JSR Clear_Cover_Text
    RTS
.Show
    LDA #BLINK_TEXT_INDEX - 1
    JSR Write_Cover_Text
    RTS
.End
    RTS
 
;==================================================
;闪烁二维码
Blink_QR_Code
    LDA Cover_Mode_Time
    CMP #$FF
    BNE .Show
    LDA PPU_Ctrl_Buf
    AND #$02
    BNE .End
    LDA Nmi_Time_Count
    AND #(1 << BLINK_QC_CODE_INTERVAL) - 1
    BNE .End
    LDA Nmi_Time_Count
    LDX #BLINK_QC_CODE_INTERVAL
    BEQ .Check
.Shift
    LSR A
    DEX
    BNE .Shift
.Check
    AND #$01
    BEQ .Show
.Clear
    LDA #24 - 1
    ;JSR Write_Cover_Text
    LDA #11 - 1
    JSR Clear_Cover_Text
    LDA #17 - 1
    JSR Clear_Cover_Text
    RTS
.Show
    LDA #25 - 1
    ;JSR Write_Cover_Text
    LDA #11 - 1
    JSR Write_Cover_Text
    LDA #17 - 1
    JSR Write_Cover_Text
    RTS
.End
    RTS
 
;==================================================
;闪烁文本
Blink_Custom_Text
    LDA Cover_Mode_Time
    CMP #$FF
    BNE .Show
    LDA PPU_Ctrl_Buf
    AND #$02
    BNE .End
    LDA Nmi_Time_Count
    AND #(1 << BLINK_CUSTOM_INTERVAL) - 1
    BNE .End
    LDA Nmi_Time_Count
    LDX #BLINK_CUSTOM_INTERVAL
    BEQ .Check
.Shift
    LSR A
    DEX
    BNE .Shift
.Check
    AND #$01
    BEQ .Show
.Clear
    LDA #03 - 1
    JSR Clear_Cover_Text
    LDA #04 - 1
    JSR Clear_Cover_Text
    RTS
.Show
    LDA #03 - 1
    JSR Write_Cover_Text
    LDA #04 - 1
    JSR Write_Cover_Text
    RTS
.End
    RTS
 
;==================================================
Init_Cover_Text
    JSR Set_Cover_Text_Info
    JSR Init_Text_By_Data
    RTS

;==================================================
Write_Cover_Text
    JSR Set_Cover_Text_Info
    JSR Write_Text_By_Buffer
    RTS

;==================================================
Clear_Cover_Text
    JSR Set_Cover_Text_Info
    JSR Clear_Text_By_Buffer
    RTS

;==================================================
Set_Cover_Text_Info
    PHA
    LDA #BANK(Text_Data_Info_Index) & BANK_DATA_MASK
    STA Text_Bank
    LDA #<Text_Data_Info_Index
    STA Text_Info_Index_L
    LDA #>Text_Data_Info_Index
    STA Text_Info_Index_H
    LDA #(Text_Data_Info_Index_End - Text_Data_Info_Index) / 2
    STA Text_Info_Count
    PLA
    RTS

    .IF 0
;==================================================
;初始化封面调色板
Init_Cover_Palette
    BIT PPU_STATUS
    LDA #$3F
    STA PPU_ADDRESS
    LDA #$00
    STA PPU_ADDRESS
    LDX #$00
    LDY #$20
.Write
    LDA .Palette_Data,X
    STA PPU_DATA
    INX
    DEY
    BNE .Write
    RTS

;--------------------------------------------------
;调色板数据
.Palette_Data
    .HEX 0F 27 37 20 0F 23 33 20 0F 21 21 20 0F 25 35 20
    .HEX 0F 20 2C 25 0F 24 20 0F 0F 24 20 0F 0F 24 20 0F
    .ENDIF

;==================================================
;初始化封面属性
Init_Cover_Attributes
    BIT PPU_STATUS
    LDA #$23
    STA PPU_ADDRESS
    LDA #$C0
    STA PPU_ADDRESS
    LDX #$00
    LDY #$40
.Write
    LDA .Attributes_Data,X
    STA PPU_DATA
    INX
    DEY
    BNE .Write
    RTS

.Attributes_Data
    .INCBIN "data/cover.attr"

    .ENDIF
